package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"net/url"
)

type NoticeController struct{}

func (that *NoticeController) Insert(ctx *fasthttp.RequestCtx) {

	data := model.TblNotices{}
	body := ctx.PostBody()

	err := json.Unmarshal(body, &data)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	admin := session.AdminGet(ctx)

	data.Id = helper.GenId()
	data.State = "1"
	data.CreatedAt = uint32(ctx.Time().Unix())

	data.Title = url.PathEscape(data.Title)
	data.Content = url.PathEscape(data.Content)

	data.CreatedUid = admin["id"]
	data.CreatedName = admin["name"]

	err = model.NoticeInsert(data)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

// 系统公告列表
func (that *NoticeController) List(ctx *fasthttp.RequestCtx) {

	page := ctx.QueryArgs().GetUintOrZero("page")
	pageSize := ctx.QueryArgs().GetUintOrZero("page_size")
	state := string(ctx.QueryArgs().Peek("state"))
	createdName := string(ctx.QueryArgs().Peek("created_name"))
	startTime := string(ctx.QueryArgs().Peek("start_time"))
	endTime := string(ctx.QueryArgs().Peek("end_time"))

	if page < 1 {
		page = 1
	}
	if pageSize < 10 {
		pageSize = 10
	}
	if pageSize > 200 {
		pageSize = 200
	}
	ex := g.Ex{}

	if state != "" && (state == "1" || state == "0") {
		ex["state"] = state
	}

	if createdName != "" {
		ex["created_name"] = createdName
	}

	data, err := model.NoticeList(uint(page), uint(pageSize), startTime, endTime, ex)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, data)
}

// 系统公告编辑
func (that *NoticeController) Update(ctx *fasthttp.RequestCtx) {

	data := model.TblNotices{}
	body := ctx.PostBody()

	err := json.Unmarshal(body, &data)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	record := g.Record{}
	if data.Title != "" {
		record["title"], _ = url.PathUnescape(data.Title)
	}

	if data.Content != "" {
		record["content"], _ = url.PathUnescape(data.Content)
	}

	if len(record) == 0 {
		helper.RetData(ctx, false, helper.NoDataUpdate)
		return
	}

	err = model.NoticeUpdate(data.Id, record)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

// 系统停用 启用 系统审核
func (that *NoticeController) UpdateState(ctx *fasthttp.RequestCtx) {

	id := string(ctx.QueryArgs().Peek("id"))
	if !helper.CtypeDigit(id) {
		helper.RetData(ctx, false, helper.IDErr)
		return
	}

	state := ctx.QueryArgs().GetUintOrZero("state")
	s := map[int]bool{
		1: true, //停用
		2: true, //启用
	}
	if _, ok := s[state]; !ok {
		helper.RetData(ctx, false, "状态错误")
		return
	}

	err := model.NoticeUpdateState(id, state)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

// 系统公告删除
func (that *NoticeController) Delete(ctx *fasthttp.RequestCtx) {

	id := string(ctx.QueryArgs().Peek("id"))
	if !helper.CtypeDigit(id) {
		helper.RetData(ctx, false, helper.IDErr)
		return
	}

	err := model.NoticeDelete(id)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	helper.RetData(ctx, true, "成功")
}

type PopupWinListParam struct {
	Nullity  string `json:"nullity"`
	FuncType string `json:"func_type"`
	JumpType string `json:"jump_type"`
	Page     uint   `json:"page" cbor:"page"`
	PageSize uint   `json:"page_size" cbor:"page_size"`
}

// 弹窗公告管理
func (that *NoticeController) PopupWinList(ctx *fasthttp.RequestCtx) {
	param := PopupWinListParam{}

	err := json.Unmarshal(ctx.PostBody(), &param)

	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}
	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	ex := g.Ex{}
	if param.Nullity != "" {
		ex["nullity"] = param.Nullity
	}

	if param.JumpType != "" {
		ex["jump_type"] = param.JumpType
	}

	if param.FuncType != "" {
		ex["func_type"] = param.FuncType
	}

	data, err := model.GetPopupWinList(param.Page, param.PageSize, ex)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, data)
}

// /更新弹窗
func (that *NoticeController) PopupWinEdit(ctx *fasthttp.RequestCtx) {
	param := model.PopupWinItem{}

	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("弹窗信息编辑操作", ctx)
	err = model.EditPopupWin(param)
	if err != nil {

		helper.RetData(ctx, false, helper.OperateFailed)
		return
	}
	helper.RetData(ctx, true, "成功")
}

// /弹窗删除
func (that *NoticeController) PopupWinDelete(ctx *fasthttp.RequestCtx) {
	id := string(ctx.QueryArgs().Peek("id"))
	if !helper.CtypeDigit(id) {
		helper.RetData(ctx, false, helper.IDErr)
		return
	}
	model.InsertLog("弹窗配置删除操作", ctx)
	err := model.DeletePopupWin(id)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that NoticeController) SuspensionList(ctx *fasthttp.RequestCtx) {
	param := model.CommonParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	s, err := model.GetSuspensionList(param)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that NoticeController) UpdateSuspension(ctx *fasthttp.RequestCtx) {
	param := model.SuspensionConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("悬浮图配置更新", ctx)
	err = model.UpdateSuspension(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that NoticeController) DeleteSuspension(ctx *fasthttp.RequestCtx) {
	param := model.SuspensionConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("悬浮图配置删除", ctx)
	err = model.UpdateSuspension(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
